<?
session_start();
define('SERVICE_URL', 'http://locify.com.server1.azhosting.sk/friends/');

define("SQL_HOST","xxx");
define("SQL_DBNAME","xxx");
define("SQL_USERNAME","xxx");
define("SQL_PASSWORD","xxx");

mysql_connect(SQL_HOST, SQL_USERNAME, 
SQL_PASSWORD);
mysql_select_db(SQL_DBNAME);

mysql_query("delete from users where time<".(time()-60));

if ($_COOKIE["nick"] <> "" AND (!isset($_SESSION["nick"]))) $_SESSION["nick"] = $_COOKIE["nick"];


$lang = getLang();

header("Pragma:no-cache");

function getLang() {
	$agent = explode('/', substr($_SERVER['HTTP_USER_AGENT'], strpos($_SERVER['HTTP_USER_AGENT'], 'Locify')));
	if(strStr($agent[2], ' ')) {
		$foo = explode(' ', $agent[2]);
		$agent[2] = $foo[0];
	}
	if($agent[0] == 'Locify' && isset($agent[2])) {
		$agent[2] = preg_replace('/^([^_]+)_.+$/', '\\1', $agent[2]);
		$lang_file = $agent[2].'.php';
		if(file_exists($lang_file)) {
			require $lang_file;
			return $lang;
		}
	}
	require 'cs.php';
	return $lang;
}


function generate_kml() {
	header("Content-Description: File Transfer");
	header("Content-Type: application/force-download; charset=UTF-8");
	header("Content-Disposition: attachment; filename=\"".$_SESSION["groupe"].".kml\"");
		
	print '<?xml version="1.0" encoding="UTF-8"?>
		<kml xmlns="http://earth.google.com/kml/2.2">
		<NetworkLink>
   			<name>'.$_SESSION["groupe"].'</name>
   			<open>1</open>
   			<visibility>1</visibility>
   			<refreshVisibility>1</refreshVisibility>
   			<Link>
   			  <href>'.SERVICE_URL.'kml.php</href>
   			  <refreshMode>onInterval</refreshMode>
   			  <refreshInterval>5</refreshInterval>
   			  <viewRefreshMode>onStop</viewRefreshMode>
   			  <viewFormat>lat=[lookatLon]&amp;lon=[lookatLat]</viewFormat>
   			</Link>
		</NetworkLink>
		</kml>';
}




function nastav_session() {
	$vysledek = mysql_query("select * from users where groupe='".$_POST["groupe"]."'");
	$barvy = array("modra", "zelena", "cervena", "zluta", "tyrkysova", "fialova", "oranzova", "hneda", "tmave_hneda", "tmave_zelena");
	$i = mysql_num_rows($vysledek);
	if ($i > 10) {
		$c = floor($i/10);
		$i = $i-($c*10);
	}
	$i=$i-1;
	$_SESSION["color"] = $barvy[$i];
	$_SESSION["nick"] = $_POST["nick"];
	setcookie("nick", $_POST["nick"]);
	$_SESSION["groupe"] = $_POST["groupe"];
	$_SESSION["heslo"] = $_POST["heslo"];
	mysql_query("UPDATE users SET color = '".$_SESSION["color"]."' WHERE nick='".$_SESSION["nick"]."' AND groupe='".$_SESSION["groupe"]."'");
	generate_kml();
}


?>
